package org.breezeiot.protodecode.protocol.qgdw3761;

import org.breezeiot.protodecode.protocol.chargingpile.MsgDcsResult;
import org.breezeiot.protodecode.protocol.qgdw3761.utils.*;
import org.breezeiot.protodecode.enums.GwAfnEnum;
import org.breezeiot.protodecode.utils.ProtocolUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2018-09-12.
 */
@Component
public class ProtocolGW09Handle {

    private static Logger logger = LoggerFactory.getLogger(ProtocolGW09Handle.class);

    private final short protocolid = 0x02;

    private final byte MSTA = 0x0D;

    public static MsgDcsResult decoder(byte[] msg) {
        // AFN
        int afn = ProtocolUtils.Unsignd(msg[12]);
        int sqe = ProtocolUtils.Unsignd(msg[13]);
        // 6个固定字节，1个控制码，7~10地址
        boolean acd = (msg[6] & 0x20) == 0 ? false : true; // 是否有重要事件等待访问???
        boolean prm = ((msg[6] & 0x40) == 0);//0表示为终端响应报文，需要上送任务
        GwAfnEnum gwAfn = GwAfnEnum.getEnmuByValue(afn);
        MsgDcsResult gwResult = null;
        try {
            switch (gwAfn) {
                case AFN_CONFIRM_CMD://确认命令:00H
                    gwResult = Gw09ComfirmUtil.parseACKData(msg, 13, acd);
                    gwResult.setCmdType(GwAfnEnum.AFN_CONFIRM_CMD.getValue());
                    break;
                case AFN_CALL_PARAM://查询参数
                    gwResult = Gw09CallParamUtil.parseCallParamData(msg, 13, acd);
                    gwResult.setCmdType(GwAfnEnum.AFN_CALL_PARAM.getValue());
                    break;
                case AFN_SETUP_PARAM://设置参数
                    break;
                case AFN_REQ_1ST_DATA://请求一类数据
                    gwResult = Gw09ClassOneUtil.parseClassOneData(msg, 13, acd);
                    gwResult.setCmdType(GwAfnEnum.AFN_REQ_1ST_DATA.getValue());
                    break;
                case AFN_REQ_2ND_DATA://请求二类数据
                    gwResult = Gw09ClassTwoUtil.parseClassTwoData(msg, 13, acd);
                    gwResult.setCmdType(GwAfnEnum.AFN_REQ_2ND_DATA.getValue());
                    break;
                case AFN_REQ_3RD_DATA://请求三类数据
                    gwResult = Gw09ClassThreeUtil.parseClassThreeData(msg, 13, acd);
                    gwResult.setCmdType(GwAfnEnum.AFN_REQ_3RD_DATA.getValue());
                    break;
                case AFN_DATA_TRANSMIT://透明转发
                    gwResult = Gw09TransmitUtil.parseTransmitData(msg, 13, acd);
                    gwResult.setCmdType(GwAfnEnum.AFN_DATA_TRANSMIT.getValue());
                    break;
                default:
                    throw new Exception("上报数据AFN不正确！");
            }
            gwResult.setSuccess(true);
        } catch (Exception e) {
            logger.error("命令解析出错, error: ", e);
            gwResult = new MsgDcsResult();
            gwResult.setSuccess(false);
            //设成确认帧
            gwResult.setCmdType(GwAfnEnum.AFN_CONFIRM_CMD.getValue());
            gwResult.setMessage("命令解析出错:" + e.getMessage());
        }
        if (prm) {
            gwResult.setPrm(true);
        }
        return gwResult;
    }

}
